ARTICLES > PHP

PHP upload multiple files Turn Back

2016-09-26 13:10:36

PHP โค้ดสำหรับฟอร์มอับโหลดไฟล์ หลายไฟล์ โดยมีการตรวจสอบนามสกุลของไฟล์ และจำกัดขนาด size
พร้อมกับเช็ค error ขณะอับโหลด

HTML

<form action="file_upload.php" method="post" enctype="multipart/form-data">

<input type="file" name="files[]">
<input type="file" name="files[]">
<input type="file" name="files[]">
 
<button type="submit" name="hasSave">Upload</button>

</form>

file-upload.php

<?PHP

if( isset($_POST['hasSave']) ){

// OR BASIC UPLOAD //////////////////////////////////////////////////////////////////////////////////////////////////

foreach ($_FILES['files']['name'] as $f => $name) {
if ($_FILES['files']['size'][$f] > 0 ) {
 
if($_FILES['files']['error'][$f] == 0){
 
move_uploaded_file($_FILES["files"]["tmp_name"][$f], $name);
 
}
}
}
 

// OR Advance UPLOAD //////////////////////////////////////////////////////////////////////////////////////////////////

    // Check the size and format
    $valid_formats = array("jpg", "png", "gif", "zip", "doc", "docx", "pdf");
    $max_file_size = 1024*2000; // 2000kb


    $path = "uploads/"; // Upload directory
    $count = 0; // upload success
    $message = array();
    $fname = array();
    $i=0; $hasfile=0;

    foreach ($_FILES['files']['name'] as $f => $name) {     
        if ($_FILES['files']['size'][$f] > 0 ) {
            // continue; // Skip file if any error found
            if($_FILES['files']['error'][$f] == 4){
                print '<script>';
                print 'alert("ไฟล์แนบ (' .($i+1). ') พลาดผิด");history.back(1);';
                print '</script>';
                die();
            }
            $hasfile++;
        }
        $i++;
    }

 

// บังคับให้แนบไฟล์ อย่างน้อย 1 ไฟล์

    if($hasfile<1){
        print '<script>';
        print 'alert("กรุณาแนบไฟล์อย่างน้อย 1 ไฟล์");history.back(1);';
        print '</script>';
        die();
    }
    
    $i=0;
    foreach ($_FILES['files']['name'] as $f => $name) {            
        if ($_FILES['files']['error'][$f] == 0) {               
            if ($_FILES['files']['size'][$f] > $max_file_size) {
                $message[] = "$name มีขนาดไฟล์มากกว่า ".($max_file_size/1024)."";
                continue; // Skip large files
            }
            elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
                $message[] = "$name ฟอร์แมตไม่ถูกต้อง";
                continue; // Skip invalid file formats
            }

        }
    }

 

    if(count($message) > 0){ // Has error found!
        foreach($message as $t){
            $txt.= $t.' ';
        }
        print '<script>alert("' .$txt. '");history.back(1);</script>';    
        die();

    }else{

        foreach ($_FILES['files']['name'] as $f => $name) {  
        
            // Change file name --Kill multiple dot(.)
            $ext = pathinfo($name, PATHINFO_EXTENSION);

            $newName =  $_SESSION['ID']."_f".$f."_".date("YmdHis").".".$ext;

            $file_path = $path.$newName;

            if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $file_path)){

                $count++; // Number of successfully uploaded file
                $fname[$f] = $file_path; // นำค่าไปใช้งาน เช่น Insert ในฐานข้อมูล
            }
        }
    }

// END //////////////////////////////////////////////////////////////////////////////

}

?>

 

Turn Back